<?php
/**
 * In this file the class '\UF\Drawing\ColorGD' is defined.
 *
 * Der Code wurde mit der Netbeans-PHP-IDE entwickelt und ist codetechnisch
 * (Codefaltung) für diese IDE optimiert.
 *
 *
 *
 * @category   UniKap-Framework
 * @package    Drawing    Drawing
 * @subpackage Core
 * @author     Ulf -UniKado- Kadner <ulfikado@gmail.com>
 * @since      2014-04-04 16:17
 * @version    0.1
 */

namespace UF\Drawing
{

    /**
     * Diese Klasse stellt eine Farbe als RGB, Hex und GD-Wert dar, sowie
     * speichert deren Deckkraft und den assoziierten Alphawert.
     *
     * @since  v0.1     */
    class ColorGD extends \UF\Drawing\Color
    {

        # <editor-fold defaultstate="collapsed" desc="C O N S T R U C T O R">

        /**
         * Inits a new instance.
         *
         * @param string|int|array $colorDefinition
         * @param int              $opacity
         */
        public function __construct( $colorDefinition, $opacity=100 )
        {
            parent::__construct( '#000000', $opacity );
            if ( \UF\TypeTool::IsInteger( $colorDefinition ) ) $this->setGdValue( $colorDefinition );
            elseif ( \is_array( $colorDefinition ) )           $this->fromArray( $colorDefinition );
            else                                               $this->setWebColor( $colorDefinition );
        }

        # </editor-fold>

        # <editor-fold defaultstate="collapsed" desc="P U B L I C   M E T H O D S">

        public final function getGdValue()
        {
            return $this->data['gd'];
        }

        public final function setGdValue( $gdValue )
        {
            $this->data['gd'] = $gdValue;
            if ( $gdValue > 16777215 )
            {
                $this->data['alpha'] = ($gdValue >> 24) & 0xFF;
                $this->data['red'] = ($gdValue >> 16) & 0xFF;
                $this->data['green'] = ($gdValue >> 8) & 0xFF;
                $this->data['blue'] = $gdValue & 0xFF;
                if ( $this->data['alpha'] == 0 ) $this->data['opacity'] = 0;
                elseif ( $this->data['alpha'] == 127 ) $this->data['opacity'] = 100;
                else $this->data['opacity'] = \intval( \floor(
                    (100 * $this->data['alpha']) / 127) );
                return;
            }
            $this->data['alpha']   = 0;
            $this->data['red']     = ($gdValue >> 16) & 0xFF;
            $this->data['green']   = ($gdValue >> 8) & 0xFF;
            $this->data['blue']    = $gdValue & 0xFF;
            $this->data['opacity'] = 100;
        }

        public function __get( $name )
        {
            if ( false !== ($value = $this->get($name)) ) return $value;
            switch ( \strtolower( $name ) )
            {
                case 'gdvalue':
                case 'gdcolor':
                    return $this->data['gd'];
                default: return false;
            }
        }

        public function __set( $name, $value )
        {
            $resetGD = true;
            switch ( \strtolower( $name ) )
            {
                case 'gdvalue': case 'gdcolor':
                    $this->setGdValue( $value );
                    $resetGD = false;
                    break;
                case 'rgb':
                    $this->setRGB($value);
                case 'r': case 'red':
                    $this->setR($value);
                    break;
                case 'g': case 'green':
                    $this->setG($value);
                    break;
                case 'b': case 'blue':
                    $this->setB($value);
                    break;
                case 'hex': case 'hexadecimal': case 'value': case 'webcolor':
                    $this->setWebColor($value);
                    break;
                case 'alpha': case 'a':
                    $this->setAlpha($value);
                    break;
                case 'o': case 'opacity':
                    $this->setOpacity($value);
                    break;
            }
            if ( $resetGD )
            {
                if (\is_null($this->data['alpha'])) $this->data['gd'] = \hexdec(
                    \str_pad(\dechex($this->data['red']), 2, 0, \STR_PAD_LEFT) .
                    \str_pad(\dechex($this->data['green']), 2, 0, \STR_PAD_LEFT) .
                    \str_pad(\dechex($this->data['blue']), 2, 0, \STR_PAD_LEFT));
                else $this->data['gd'] = \bindec(
                    \decbin(  $this->data['alpha']  ) .
                    \decbin(  \hexdec( \substr($this->data['hex'], 1) )  )  );
            }
        }

        public function createGdValue()
        {
            return $this->data['gd'];
        }

        # </editor-fold>

    }

}

